package com.bawei.bitmapdemo;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    private ImageView mImageView;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView = findViewById(R.id.iv);
        requestPermissions(new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        },101);
//        load1();
//        load2();
//        int i = dip2px(this, 2.0f);
//        Log.d("ytx", "dp转px: "+i);

        load4();
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public  int dip2px(Context context, float dpValue) {
        //获得当前手机屏幕dpi
        final float densityDpi = context.getResources().getDisplayMetrics().densityDpi;
        //获得当前手机屏幕分辨率
        final float widthPixels = context.getResources().getDisplayMetrics().widthPixels;
        final float heightPixels = context.getResources().getDisplayMetrics().heightPixels;
        Log.d("ytx", "densityDpi: "+densityDpi);
        Log.d("ytx", "widthPixels: "+widthPixels);
        Log.d("ytx", "heightPixels: "+heightPixels);
        //获得当前手机屏幕比例 1dp = xxx px
        final float scale = context.getResources().getDisplayMetrics().density;
        Log.d("ytx", "scale比例: "+scale);
        return (int) (dpValue * scale + 0.5f);


    }

    
    //默认ARGB_8888 占4个字节
    public void load1() {
        InputStream inputStream = null;
        try {
            inputStream = getAssets().open("shi.jpg");
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            Log.d("ytx", "ARGB_8888：bitmap所占的内存: "+bitmap.getByteCount());
//            mImageView.setImageBitmap(bitmap);
//            //质量压缩 50%
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream("/storage/emulated/0/Pictures/a1.jpg"));
            bitmap.compress(Bitmap.CompressFormat.JPEG,60,new FileOutputStream("/storage/emulated/0/Pictures/a2.jpg"));
            bitmap.compress(Bitmap.CompressFormat.JPEG,20,new FileOutputStream("/storage/emulated/0/Pictures/a3.jpg"));


        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //RGB_565 2个字节
    public void load2()  {
        InputStream inputStream = null;
        try {
            inputStream = getAssets().open("shi.jpg");
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;//设置像素存储方式 一个像素2个字节
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream,new Rect(0,0,0,0),options);
            Log.d("ytx", "RGB_565：bitmap所占的内存: "+bitmap.getByteCount());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    public void load3(){
        Bitmap bitmap = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/a1.jpg");
        mImageView.setImageBitmap(bitmap);
        Log.d("ytx", "没有进行尺寸压缩: "+bitmap.getByteCount());
        //132710400
    }
    //尺寸压缩：影响内存大小，防止OOM
    public void load4(){
        //第一次：只采集宽和高
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile("/storage/emulated/0/Pictures/a1.jpg",options);
        int outWidth = options.outWidth;
        int outHeight = options.outHeight;
        int size = 1;
        while ((outHeight/size > 200)||(outWidth/size >200)){
            size*=2;
        }
        //第二次：采像素
        options.inJustDecodeBounds = false;
        options.inSampleSize = size;
        Bitmap bitmap = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/a1.jpg", options);
        mImageView.setImageBitmap(bitmap);
        Log.d("ytx", "进行尺寸压缩: "+bitmap.getByteCount());
    }
}